//
//  TopScoreViewController.m
//  618
//
//  Created by linda on 13-7-14.
//
//

#import "TopScoreViewController.h"
#import "Score.h"
#import "Sstyle.h"

@interface TopScoreViewController ()

@end

@implementation TopScoreViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //self.navigationController.navigationBarHidden = NO;
    
    UIImage  *cancelImg = [UIImage imageNamed:@"cancel_button.png"];
    UIBarButtonItem* cancelBtnItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain
                                                                     target:self action:@selector(cancelAction:)];
    //cancelBtnItem.image = cancelImg;
    self.navigationItem.leftBarButtonItem = cancelBtnItem;
    [cancelBtnItem release];

    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [cancelBtn setImage:cancelImg forState:UIControlStateNormal];
    cancelBtn.frame = (CGRect){{10,self.view.frame.size.height - cancelImg.size.height-50},cancelImg.size};
    [cancelBtn addTarget:self action:@selector(cancelAction:)forControlEvents:UIControlEventTouchDown];

    cancelBtn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleRightMargin;

    [self.view addSubview:cancelBtn];
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSData * topScores = [defaults objectForKey:@"topScores"];
    if (topScores) {
        scoreArr = [[NSKeyedUnarchiver unarchiveObjectWithData:topScores] retain];
        
    }
    else
    {
        scoreArr = [[NSMutableArray alloc] init];
    }
    
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [scoreArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    
    cell.imageView.image = [UIImage imageNamed:@"start.png"];
    cell.accessoryType = UITableViewCellAccessoryNone;
    Score *temp = (Score *)[scoreArr objectAtIndex:indexPath.row];
    //cell.textLabel.text = [NSString stringWithFormat:@"%d     %@       %@",temp.score,temp.name,temp.recordTime];
    [cell textLabel].font = [Sstyle klavikaBold:16.0];
    cell.detailTextLabel.font = [Sstyle klavikaRegular:12];
    [cell textLabel].text = [NSString stringWithFormat:@"%d         %@",temp.score,temp.name];;
    cell.detailTextLabel.text	= temp.recordTime;
    cell.textLabel.textColor = [UIColor darkGrayColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    return cell;
}

- (IBAction) cancelAction:(id)sender
{

    self.navigationController.navigationBarHidden = YES;
    if (self.navigationController) {
		[self.navigationController popViewControllerAnimated:YES];
	}
//    else {
//		[self dismissModalViewControllerAnimated:YES];
//	}
    
    //[[NSNotificationCenter defaultCenter] postNotificationName:@"removeHighScores" object:@""];

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
}

@end
